package com.yipao.db.service.devinfo;

import com.github.pagehelper.PageHelper;
import com.yipao.db.constant.DbConstant;
import com.yipao.db.dao.dev.DevMaterialSlotMapper;
import com.yipao.db.dao.dev.DevInfoMapper;
import com.yipao.db.domain.devinfo.DevMaterialSlot;
import com.yipao.db.domain.devinfo.DevInfo;
import com.yipao.db.domain.devinfo.DevInfoExample;
import com.yipao.db.domain.excel.ExportDevInfoVo;
import com.yipao.db.util.DeleteStatusConstant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 设备
 *
 * @author voyboy
 */
@Service
public class DevInfoService {

    @Resource
    private DevInfoMapper devInfoMapper;
    @Resource
    private DevMaterialSlotMapper devFormulaSlotMapper;

    /**
     * 查询所有
     *
     * @param scenes    投放场景
     * @param devCode   设备编号
     * @param devStatus 设备编号
     * @param start
     * @param end
     * @param page      第几页
     * @param limit     一页显示多少
     * @param sort
     * @param order
     * @return
     */
    public List<DevInfo> findAll(String devStatus, String statusCode, String scenes, String devCode,
                                 Date start, Date end, Integer page, Integer limit, String sort, String order) {
        DevInfoExample example = new DevInfoExample();
        DevInfoExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(scenes)) {
            criteria.andScenesLike("%" + scenes + "%");
        }
        if (!StringUtils.isEmpty(devCode)) {
            criteria.andDevCodeEqualTo(devCode);
        }
        if (!StringUtils.isEmpty(devStatus)) {
            criteria.andDevStatusEqualTo(devStatus);
        }

        if (!StringUtils.isEmpty(statusCode)) {
            criteria.andStatusCodeEqualTo(statusCode);
        }
        if (!StringUtils.isEmpty(start)) {
            criteria.andHappenedTimeGreaterThan(start + "");
        }
        if (!StringUtils.isEmpty(end)) {
            criteria.andHappenedTimeLessThan(end + "");
        }
        if (!StringUtils.isEmpty(statusCode)) {
            criteria.andStatusCodeEqualTo(statusCode);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        criteria.andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        PageHelper.startPage(page, limit);
        return devInfoMapper.selectByExample(example);
    }


    /**
     * 新增设备
     *
     * @param
     */
    public int insert(DevInfo devInfo) {
        Integer maxDevCode = devInfoMapper.getMaxDevCode();
        devInfo.setAddTime(LocalDateTime.now() + "");
        devInfo.setUpdateTime(devInfo.getAddTime());
        int devCode;
        if (maxDevCode == null) {
            devCode = 1;
        } else {
            devCode = maxDevCode + 1;
        }
        devInfo.setDevCode(String.valueOf(devCode));
        devInfo.setDeleted(DeleteStatusConstant.NOT_DELETED);
        int result = devInfoMapper.insert(devInfo);
        if (result > 0) {
            for (int i = 1; i <= DbConstant.DEV_SLOT_NUM; i++) {
                // 新增5个设备原料卡槽
                devFormulaSlotMapper.insert(new DevMaterialSlot(devInfo.getId(), i));
            }
        }
        return result;
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public DevInfo selectByPrimaryKey(Integer id) {
        return devInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新
     *
     * @param devInfo
     */
    public int update(DevInfo devInfo) {
        devInfo.setUpdateTime(LocalDateTime.now() + "");
        return devInfoMapper.updateByPrimaryKeySelective(devInfo);
    }

    /**
     * 根据ID删除设备
     *
     * @param id
     */
    public int delete(Integer id) {
        return devInfoMapper.updateByPrimaryKeySelective(new DevInfo(id, DeleteStatusConstant.IS_DELETED));
    }

    /**
     * 多条记录删除
     *
     * @param ids
     */
    public void deleteIds(Integer[] ids) {
        DevInfo devInfo;
        for (Integer id : ids) {
            devInfo = new DevInfo(id, DeleteStatusConstant.IS_DELETED);
            devInfoMapper.updateByPrimaryKeySelective(devInfo);
        }
    }

    /**
     * 根据条件查询
     *
     * @param devCode 设备编号
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<DevInfo> selectByExample(String devCode, String scenes, String employee,
                                         Integer page, Integer limit, String sort, String order) {

        DevInfoExample example = new DevInfoExample();
        DevInfoExample.Criteria criteria = example.createCriteria();

        if (null != devCode) {
            criteria.andDevCodeEqualTo(devCode);
        }
        if (!StringUtils.isEmpty(scenes)) {
            criteria.andScenesLike(scenes);
        }
        if (!StringUtils.isEmpty(employee)) {
            criteria.andEmployeeLike("%" + employee + "%");
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        criteria.andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        PageHelper.startPage(page, limit);
        return devInfoMapper.selectByExample(example);

    }

    public List<DevInfo> selectByDevId(Integer devId, Integer page, Integer limit, String sort, String order) {

        DevInfoExample example = new DevInfoExample();
        DevInfoExample.Criteria criteria = example.createCriteria();

        if (null != devId) {
            criteria.andIdEqualTo(devId);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        criteria.andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        PageHelper.startPage(page, limit);
        return devInfoMapper.selectByExample(example);

    }

    /**
     * 获取所有状态码(去重)
     *
     * @return
     */
    public List<Integer> getAllStatusCode() {
        return devInfoMapper.getAllStatusCode();
    }

    /**
     * 获取所有故障状态(去重)
     *
     * @return
     */
    public List<Integer> getAllDevStatus() {
        return devInfoMapper.getAllDevStatus();
    }

    /**
     * 根据时间查询
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<DevInfo> getByTime(LocalDateTime startTime, LocalDateTime endTime) {
        DevInfoExample example = new DevInfoExample();
        DevInfoExample.Criteria criteria = example.createCriteria();
        if (startTime != null) {
            criteria.andHappenedTimeGreaterThan(startTime + "");
        }
        if (endTime != null) {
            criteria.andHappenedTimeLessThan(endTime + "");
        }
        return devInfoMapper.selectByExample(example);
    }

    /**
     * 根据故障码搜索
     *
     * @param statusCode
     * @return
     */
    public List<DevInfo> findDevByStatusCode(String statusCode) {

        DevInfoExample example = new DevInfoExample();
        DevInfoExample.Criteria criteria = example.createCriteria();
        criteria.andStatusCodeEqualTo(statusCode);


        return devInfoMapper.selectByExample(example);
    }

    public List<ExportDevInfoVo> findAllExcel() {
        return devInfoMapper.findAllExcel();
    }

    public Integer getIdByImei(String imei) {
        return devInfoMapper.getIdByImei(imei, DeleteStatusConstant.NOT_DELETED);
    }

    /**
     * 根据设备id获取机器识别码
     *
     * @param id    机器识别码
     * @return
     */
    public String getImeiById(Integer id) {
        return devInfoMapper.getImeiById(id, DeleteStatusConstant.NOT_DELETED);
    }

    /**
     * 根据imei查询设备是否存在
     *
     * @param imei
     * @return
     */
    public Integer queryDevCountByImei(String imei) {
        return devInfoMapper.queryDevCountByImei(imei, DeleteStatusConstant.NOT_DELETED);
    }

    /**
     * 查询状态为不删除的所有设备是iMei
     * @return
     */
    public List<String> queryAllDev(){
        return devInfoMapper.queryAllDev();
    }
}
